package cn.szzsi.interceptor;

import cn.szzsi.common.kit.CommonUtil;
import cn.szzsi.common.model.*;
import com.jfinal.aop.Interceptor;
import com.jfinal.aop.Invocation;
import com.jfinal.core.Controller;
import com.jfinal.kit.StrKit;
import org.apache.commons.lang3.StringUtils;

import javax.servlet.http.Cookie;

public class MemberPcInterceptor implements Interceptor {

	public void intercept(Invocation ai) {
		Controller c = ai.getController();
        String requestUrl=c.getRequest().getRequestURI();//访问路径
        if(isPassUrl(requestUrl)){
            ai.invoke();
            return;
        }

		String loginMemberId = c.getSessionAttr(Member.LOGIN_MEMBER_ID_SESSION_NAME);//获取当前登录用户
        String shopId=c.getAttr("shopId");
        if (StrKit.notBlank(loginMemberId)) {//用户已经登录
			Member member = Member.dao.findById(loginMemberId);
            setCommonInfo(c,shopId,member);
		}else {
            //用户未登录
            clearCookie(c);
            setRedirectUrl(c);
            setCommonInfo(c,shopId,null);
            //若用户没有登录,判断是不是weiixn登录
            //SiteConfig siteConfig=SiteConfig.dao.findByShopId(shopId);
            c.redirect("/index.html");//个人中心
            return;
        }
        ai.invoke();
	}

    /**
     * 设置一些页面常用的通用变量，这些变量尽可能放到缓存中
     * @param c
     * @param shopId
     * @param member
     */
    private void setCommonInfo(Controller c, String shopId, Member member){
        if(member != null) {
            c.setAttr("loginMember", member);
        }
        if(StrKit.notBlank(shopId)){
            c.setAttr("businessTopNavigationList", Navigation.dao.getBusinessTopNavigationList(shopId));
            c.setAttr("businessMiddleNavigationList", Navigation.dao.getBusinessMiddleNavigationList(shopId));
            c.setAttr("businessBottomNavigationList", Navigation.dao.getBusinessBottomNavigationList(shopId));
            c.setAttr("businessPictureFriendLinkList", Friendlink.dao.getBusinessPictureFriendLinkList(shopId));
            c.setAttr("businessTextFriendLinkList", Friendlink.dao.getBusinessTextFriendLinkList(shopId));
            c.setAttr("allArticleList", Article.dao.getBusinessArticleList(shopId));
            c.setAttr("footer", Footer.dao.getAll());
        }

    }

    private boolean isPassUrl(String url){
        boolean flag = false;
        String[] urls = {"alipayNotice","alipayReturn","wxNotice","getUserInfo"};
        for(String s:urls){
            if(url.contains(s)){
                flag = true;
            }
        }
        return flag;
    }



    /**
     * 清除cookie
     * @param c
     * @return
     */
    public void clearCookie(Controller c){
        Cookie loginMemberUsernameCookie = new Cookie(Member.LOGIN_MEMBER_USERNAME_COOKIE_NAME, null);
        loginMemberUsernameCookie.setPath(c.getRequest().getContextPath() + "/");
        loginMemberUsernameCookie.setMaxAge(0);
        c.getResponse().addCookie(loginMemberUsernameCookie);
    }

    /**
     * 设置跳转的url
     * @param c
     */
    public void setRedirectUrl(Controller c){
        String redirectionUrl = c.getRequest().getRequestURL().toString();
        String queryString = c.getRequest().getQueryString();
        if (StringUtils.isNotEmpty(queryString)) {
            redirectionUrl += "?" + queryString;
        }
        c.setSessionAttr(Member.LOGIN_REDIRECTION_URL_SESSION_NAME,redirectionUrl);
    }


}
